package com.liting.uitest.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liting.uitest.entity.FileInfo;
import com.liting.uitest.entity.InteractRecord;
import com.liting.uitest.model.BaseRes;
import com.liting.uitest.model.ReactionResult;
import com.liting.uitest.service.InteractRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Slf4j
@Api(tags = "交互评价")
public class InteractController {
    @Autowired
    private InteractRecordService interactService;


    @PostMapping(value = "/interact/file/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation("交互评价结果文件上传,files可以上传多个文件，type定义 1：GOMS 2：反应时")
    public BaseRes<InteractRecord> uploadRectFile(@RequestPart("files") MultipartFile[] files, @RequestPart("type") String type) {
        try {
            InteractRecord result = null;
            if (type.equals("1")) {
                result = interactService.uploadRectFile(files, InteractRecord.InteractType_GOMS);
            } else {
                result = interactService.uploadRectFile(files, InteractRecord.InteractType_Fanying);
            }
            return new BaseRes<>(result);
        } catch (Exception ex) {
            log.error("uploadRectFile error", ex);
            return new BaseRes<>(null, 1, "反应时结果文件上传失败");

        }
    }

//    @GetMapping("/interact/last")
//    @ApiOperation("交互评价在beginTime之后最接近的一条记录，beginTime为用户发起交互评价的时间，格式YYYYMMDD HH:mm:SS")
//    public BaseRes<InteractRecord>  getResult(@RequestParam("beginTime") String  beginTime) {
//        try {
//            InteractRecord result = interactService.getLastResult(beginTime);
//            return new BaseRes<>(result);
//        }catch(Exception ex){
//            log.error("getResult error",ex);
//            return new BaseRes<>(null,1,"交互评价在beginTime之后最接近的一条记录查询失败");
//
//        }
//    }

    @GetMapping("/interact/record")
    @ApiOperation("交互评价在beginTime之后的记录列表，beginTime为用户发起交互评价的时间，格式yyyy-MM-dd HH:mm:ss; type定义 1：GOMS 2：反应时 ")
    public BaseRes<?> getResult(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String beginTime,
            @RequestParam(required = false) String type

    ) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = 5;

        try {
            IPage<InteractRecord> result = interactService.getList(pageNum, pageSize, type, beginTime);
            return new BaseRes<>(result);
        } catch (Exception ex) {
            log.error("getResult error", ex);
            return new BaseRes<>(null, 1, "交互评价记录列表查询失败");

        }

    }

    @GetMapping("/interact/record/info")
    @ApiOperation("交互评价详情 ")
    public BaseRes<?> getRecordInfo(
            @RequestParam Long id
    ) {
        try {
            InteractRecord result = interactService.getRecordInfo(id);
            return new BaseRes<>(result);
        } catch (Exception ex) {
            log.error("getRecordInfo error", ex);
            return new BaseRes<>(null, 1, "交互评价详情查询失败");

        }

    }


}
